package com.java.controller;

import com.java.model.Result;
import com.java.model.Role;
import com.java.service.RolePermissionService;
import com.java.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 角色页面
     * @return
     */
    @GetMapping("/list")
    public String list() {
        return "role/list";
    }

    /**
     * 角色查询
     * @param role
     * @return
     */
    @PostMapping("/toList")
    @ResponseBody
    public Result toList(Role role) {
        return roleService.toList(role);
    }

    /**
     * 角色新增页面
     * @return
     */
    @GetMapping("/add")
    public String add() {
        return "role/add";
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody Role role) {
        return roleService.save(role);
    }

    /**
     * 角色修改页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/edit")
    public String edit(int id, Model model) {
        // 获取id
        Role role = roleService.get(id);
        model.addAttribute("role", role);
        return "role/edit";
    }

    /**
     * 角色修改
     * @param role
     * @return
     */
    @PostMapping("/modify")
    @ResponseBody
    public Result modify(@RequestBody Role role) {
        return roleService.modify(role);
    }

    /**
     * 删除角色信息
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(int[] ids){
        return roleService.remove(ids);
    }

    /**
     * 分配权限页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPermission")
    public String editPermission(Model model, int id) {
        model.addAttribute("id", id);
        return "role/editPermission";
    }

    @GetMapping("/listPermission")
    @ResponseBody
    public Result listPermission(int roleId) {
        return roleService.listPermission(roleId);
    }
}
